package com.ruoyi.system.util;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @author lixbin
 * @ClassName ExportUtil
 * @description: TODO
 * @datetime 2023年 08月 13日 14:57
 * @version: 1.0
 */
public class ExportUtil {

    public static void exportPdf(String url, String endId, String tmpFilePath, String name, HttpServletResponse response) throws Exception {
        byte[] img = WebDriverUtil.screenshotForBytes(url, endId);
        File pdf = new File(tmpFilePath);
        PdfUtil.createPdfFromImg(img, pdf);
        response.setHeader("content-type", "application/pdf");
        response.setContentType("application/pdf");
        // 下载文件能正常显示中文
        response.setHeader("Content-Disposition",
            "attachment;filename=" + URLEncoder.encode(name + ".pdf", "UTF-8"));
        FileInputStream in = new FileInputStream(pdf);
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[1024];
        int n;
        while ((n = in.read(b)) != -1) {
            out.write(b, 0, n);
        }
        out.flush();
        in.close();
        out.close();

        pdf.delete();
    }
}
